/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import static cn.ac.iie.di.wx.statweb.action.NewsStatWebAction.logger;
import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class SearchEnginStatWebAction {

    private String trunk;
    static Logger logger = Logger.getLogger(SearchEnginStatWebAction.class.getName());

    public String se() {
        HttpServletRequest request = ServletActionContext.getRequest();
        trunk = request.getParameter("trunk");
        return "success";
    }

    public String se_rt_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String seType = request.getParameter("seType");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        String rt_path = "";
        String rt_web_path = "";
        String trunkval = request.getParameter("trunk");

        if (seType.equals("trs")) {
//            rt_path = PicCopy.readProperties("se.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("se.rt.tail");
            if (trunkval.equals("yizhuang")) {//默认为数北
                rt_path = PicCopy.readProperties("se.head") + "yizhuang/" + DateUtil.getToDayDate_1() + PicCopy.readProperties("se.rt.tail");
            }
            rt_web_path = PicCopy.readProperties("webapp.se.path");
        } else {
//            rt_path = PicCopy.readProperties("se.mpp.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("se.mpp.rt.tail");
            if (trunkval.equals("yizhuang")) {
                rt_path = PicCopy.readProperties("se.mpp.head") + "yizhuang/" + DateUtil.getToDayDate_1() + PicCopy.readProperties("se.mpp.rt.tail");
            }
            rt_web_path = PicCopy.readProperties("webapp.se.mpp.path");
        }
        File file = new File(rt_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        if (files == null) {
            logger.info("this is no file in " + rt_path);
            out.print("0");
        } else {
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
                String filePath = System.getProperty("user.dir") + "/" + rt_web_path + "4/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                logger.info(filePath);
                } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    //删除原有的
                    if (f.delete()) {
                        f.createNewFile();
                    }
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
        }
        out.close();
        return "success";
    }

    public String se_daily_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("se_daily_data");
        String head = request.getParameter("head");
        String tail = request.getParameter("tail");
        String webpath = request.getParameter("webpath");
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/sina/" + selectedDate + "/24");
        String trunkval = request.getParameter("trunk");
        String home_path = PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail);//获取根目录
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties(head) + "yizhuang/" + selectedDate + PicCopy.readProperties(tail);//区分数北亦庄
        }
        File file = new File(home_path);
        logger.info(home_path);

        File[] files = file.listFiles();
        if (files == null) {
            logger.info("this is no file in " + PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));
            out.print("0");
        } else {

            String fileStrArray = "";
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                logger.info(filename);
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/24/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
        }
        out.close();
        return "success";

    }

    public String se_weekly_data() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("se_weekly_data");
        String head = request.getParameter("head");
        String tail = request.getParameter("tail");
        String webpath = request.getParameter("webpath");
        
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File(PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));
        String trunkval = request.getParameter("trunk");
        String home_path = PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail);//获取根目录
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties(head) + "yizhuang/" + selectedDate + PicCopy.readProperties(tail);//区分数北亦庄
        }
        File file = new File(home_path);
        logger.info(home_path);
        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
//            logger.info(System.getProperty("user.dir"));
//            logger.info(files.length);
            if (files == null) {
                logger.info("this is no file in " + PicCopy.readProperties(head) + selectedDate + PicCopy.readProperties(tail));
            } else {

                for (int i = 0; i < files.length; i++) {
                    String filename = files[i].getName();
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
//                String filePath = System.getProperty("user.dir") + "/" + "webapps/ban-ycl-wx-stat-web-1.0/sinaimages/" + selectedDate + "/168/";
                    String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/168/";

                    File fileDir = new File(filePath);
                    if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                        fileDir.mkdirs();
//                    logger.info(filePath);
                    } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                        //清空原有的文件

                    }
                    File f = new File(filePath + filename);
                    if (f.exists()) {
                        PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                    } else {
                        f.createNewFile();
//                logger.info("create file");
                        PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                    }
                }
                String[] fs = file.list();
                Arrays.sort(fs);
                for (int i = 0; i < fs.length; i++) {
                    fileStrArray += fs[i] + ";";
                }
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            logger.info("there is no this fileDirectory or File");
            out.print("");
            out.close();
            return "success";
        }
    }

    public String getTrunk() {
        return trunk;
    }

    public void setTrunk(String trunk) {
        this.trunk = trunk;
    }

}
